<?php
$this->breadcrumbs=array(
	'Issues'=>array('index'),
	'Assign',
);

$this->menu=array(
	array('label'=>'List Issue', 'url'=>array('index')),
	array('label'=>'Create Issue', 'url'=>array('create')),
);
?>

<h1>Resolving page</h1>
<h3>Review/resolve/submit the answer for each question which assigned by checker.</h3>

<div class="grid-view">
<table border="1" class="items">
	<thead>
		<tr>
			<th>Issue</th>
			<th>Bài giải</th>		
			<th>Trạng thái bài giải</th>
			<th>Thời hạn submit bài giải</th>
			<th>Action</th>		
		</tr>
	</thead>
	<tbody>
	
	<?php
			$this->widget('zii.widgets.CListView', array(
			'id'=>'assign-issue-list',
			'dataProvider'=>$model->resolve($ID),
			'pager'=>array
			(
				'firstPageLabel' => '&lt;&lt;',
		    'prevPageLabel'  => '&lt;',
		    'nextPageLabel'  => '&gt;',
		    'lastPageLabel'  => '&gt;&gt;',
				'header'=>'',
			),
			'emptyText'=>'Không có dữ liệu.',
			'itemView'=>'_view_resolve',
		)); ?>
	
		</tbody>
</table>	
</div>

	<!-- Dialog for add/update answer -->
		<div id="dialog-form" class="dialog-form" title="Update bài giải:">
		  <p class="validateTips">All form fields are required.</p>		 
		  <form>
		  <fieldset>
		    <label for="issueID">IssueID</label>
		    <input disabled="disabled" type="text" name="issueID" id="issueID"  value="" class="text ui-widget-content ui-corner-all" />
		    <label for="num">Thread</label>
		    <input  disabled="disabled"  type="text" name="num" id="num" value="" class="text ui-widget-content ui-corner-all" />
		    <label for="content">content</label>
		    <input type="text" name="content" id="content1" value="" class="text ui-widget-content ui-corner-all" />
		  </fieldset>
		  </form>
		</div>		


<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/tables.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/simplemodal/basic.css">
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/countdown.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/jquery.simplemodal.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/basic.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/popup.js"></script>

<script type="text/javascript">
$(function() {
	$(".refresh").click(function() {
		console.log($(this));
		$(this).parent().children(".btn-success").removeAttr("disabled").removeClass("disabled");
		$(this).parent().children(".btn-inverse").removeAttr("disabled").removeClass("disabled");
	});
	
	$(".countdown").each(function() {
		if($(this).html()>0) {
			var arr_bt = $(this).attr("id").split("_");
		  var bt = arr_bt[1];
		  var issueID = arr_bt[2];
		  timer(
				  bt,
				  1000*($(this).html()),
				  function(timeleft) {

					  var time = "<button class='disabled btn btn-info'><i class='icon-time icon-white'></i>"+timeleft+"</button>"
						$("#countdown_"+bt+"_"+issueID).html(time);
						
					},
					function() {
						submitResolve(issueID, bt);
					}
			);
		}
	});	
	
	
	$("#dialog-form" ).dialog({
	    autoOpen: false,
	    height: 600,
	    width: 800,
	    modal: true,
	    buttons: {
	      "Thêm": function() {	     
	    	  updateAnswerContent();
	         $( this ).dialog( "close" );	       
	      },
	      Cancel: function() {
	        $( this ).dialog( "close" );
	      }
	    },
	    close: function() {
	    
	    }
	  });	
});

function submitResolve(issueID, answerID) {
	var resolverID = <?php echo Yii::app()->user->id?>;
	$.ajax({
		url: baseUrl + "/admin/issue/setResolveSubmit",
		data: {issueID: issueID, resolverID: resolverID, answerID: answerID},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {		
				$("#countdown_"+answerID+"_"+issueID).html("<button class='disabled btn btn-warning'><i class='icon-time icon-white'></i> Hết giờ</button>");
			}			
		}
	});
}

function beforeResolve(issueID,num,status) {
	var resolverID = $("#issue_"+issueID+"_"+num).val();
	$.ajax({
		url: baseUrl + "/admin/issue/setResolve",
		data: {issueID: issueID, status: status, num: num},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {				
				$("#"+status+"_"+issueID+"_"+num).addClass("disabled").attr("disabled","disabled");
			
			}
		}
	});
}

function addAnswer(issueID,num,status,dialogID) {

	$("#"+status+"_"+issueID+"_"+num).addClass("disabled").attr("disabled","disabled");

	$("#issueID" ).val(issueID);
	$("#num" ).val(num);				
  $("#"+dialogID).dialog( "open" );
	/*
	$.ajax({
		url: baseUrl + "/admin/issue/addAnswer",
		data: {issueID: issueID, status: status, num: num, dialogID:dialogID},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {		
				
				
			}
		}
	});
	*/
}

function updateAnswerContent() {

	var issueID = $("#issueID" ).val();
	var num = $("#num" ).val();				
	var content= $("#content1" ).val();
 alert(issueID);
 alert(num);
 alert(content);
	
	$.ajax({
		url: baseUrl + "/admin/issue/updateAnswer",
		data: {issueID: issueID, num: num, content:content},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {		
			
				$("#issueID" ).val('');
				$("#num" ).val('');				
				$("#content").val('');
			  $("#"+dialogID).dialog( "close" );
			}
		}
	});
}




function beforeAddAnswer(dialogID,issueID,num,status) {
	 $("#"+dialogID).dialog( "open" );
	 addAnswer(issueID,num,status);
}

</script>